//
//  LKChooseBankCardViewController.m
//  YLHelper
//
//  Created by she1323 on 2018/5/4.
//  Copyright © 2018年 she1323. All rights reserved.
//



#import "LKChooseBankCardViewController.h"
#import "LKOrderEditBottomView.h"
#import "LKChooseBankCardCell.h"
#import "LKChooseBankCardDetailViewController.h"
#import "LKChooseBankCardListViewController.h"
#import "LKWorkOrderListDetailCell.h"
#import "LKWorkOrderListDetailPictureCell.h"
#import "LKOrderEditTopView.h"
#import "LKDiscountTicketViewController.h"
@interface LKChooseBankCardViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)LKBaseTableview *tableview;
@property(nonatomic,strong)LKOrderEditBottomView *bottomView;
@property(nonatomic,strong)NSMutableArray *valuesArray;
@property(nonatomic,strong)NSArray *titlesArray;
@end

@implementation LKChooseBankCardViewController
-(LKBaseTableview *)tableview{
    if (!_tableview) {
        WS(weakSelf)
        _bottomView = [[[NSBundle mainBundle] loadNibNamed:@"LKOrderEditBottomView" owner:self options:nil] lastObject];
        [self.view addSubview:_bottomView];
        [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self.view);
            make.height.mas_equalTo(69);
        }];
        [[_bottomView.forwardBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }];
        [[_bottomView.nextBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            LKDiscountTicketViewController *vc = [[LKDiscountTicketViewController alloc] init];
            vc.addOrderModel = weakSelf.addOrderModel;
            [weakSelf.navigationController pushViewController:vc animated:YES];
        }];
        _tableview = [LKBaseTableview setTableviewWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT + 10, WIDTH , HEIGHT - NAVIGATION_BAR_HEIGHT - 10 - 70) style:UITableViewStylePlain registercellClasss:@[@"LKWorkOrderListDetailCell",@"LKWorkOrderListDetailPictureCell"]];
        [self.view addSubview:_tableview];
        _tableview.delegate = self;
        _tableview.backgroundColor = cellHeaderBgColor;
        _tableview.dataSource = self;
        _tableview.tableFooterView = [[UIView alloc] init];
    }
    return _tableview;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (self.isOrderUpdate) {
        self.tableview.frame = CGRectMake(0, NAVIGATION_BAR_HEIGHT + 10, WIDTH , HEIGHT - NAVIGATION_BAR_HEIGHT - 10);
        [self.bottomView removeFromSuperview];
        NSString *bankCode = [NSString stringWithFormat:@"%@%@",@"账户卡号：",self.addOrderModel.PayAccountCode];
        NSString *bankName = [NSString stringWithFormat:@"%@%@",@"开户行：",self.addOrderModel.PayBankName];
        NSString *BankBranch = [NSString stringWithFormat:@"%@%@",@"开户行支行：",self.addOrderModel.PayBankBranch];
        
        NSString *UsrBankName = [NSString stringWithFormat:@"%@%@",@"户名：",self.addOrderModel.PayAccountName];
        
        NSString *bankphotoUrl = [NSString stringWithFormat:@"%@",@"银行卡正面复印件："];
        NSString *bankCity = [NSString stringWithFormat:@"%@%@",@"开户行支行省市：", self.addOrderModel.PayBankCity];

        NSArray *paybankArra;
        if ([self.addOrderModel.IdentitfyCategory isEqual:@"1"]) {
            paybankArra = @[bankCode,UsrBankName,bankName,BankBranch,bankCity];
        }else{
            paybankArra = @[bankCode,UsrBankName,bankName,BankBranch,bankCity,bankphotoUrl];
        }
        [self.valuesArray replaceObjectAtIndex:0 withObject:paybankArra];
        NSString *bankCode1 = [NSString stringWithFormat:@"%@%@",@"账户卡号：",self.addOrderModel.RepayAccountCode];
        NSString *bankName1 = [NSString stringWithFormat:@"%@%@",@"开户行：",self.addOrderModel.RepayBankName];
        NSString *BankBranch1 = [NSString stringWithFormat:@"%@%@",@"开户行支行：",self.addOrderModel.RepayBankBranch];
        
        NSString *UsrBankName1 = [NSString stringWithFormat:@"%@%@",@"户名：",self.addOrderModel.RepayAccountName];
        NSString *bankphotoUrl1 = [NSString stringWithFormat:@"%@",@"银行卡正面复印件："];
        NSString *bankCity1 = [NSString stringWithFormat:@"%@%@",@"开户行支行省市：", self.addOrderModel.RepayBankCity];
        NSArray *repaybankArra;
        if ([self.addOrderModel.IdentitfyCategory isEqual:@"1"]) {
            repaybankArra = @[bankCode1,UsrBankName1,bankName1,BankBranch1,bankCity1];
        }else{
           repaybankArra = @[bankCode1,UsrBankName1,bankName1,BankBranch1,bankCity1,bankphotoUrl1];
        }
        [self.valuesArray replaceObjectAtIndex:1 withObject:repaybankArra];
        [self.tableview reloadData];
    }else{
         self.tableview.estimatedRowHeight = 200;
    }
   
    // Do any additional setup after loading the view.
}
-(void)setNavgationBackBtn{
    [super setNavgationBackBtn];
    self.navigationItem.title = @"选择银行卡";
}
-(void)back{
    if (self.isOrderUpdate) {
        if (self.sendAddOrderModel) {
            self.sendAddOrderModel(self.addOrderModel);
            [self.navigationController popViewControllerAnimated:YES];
        }
    }else{
        [super back];
    }
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.titlesArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *arra = self.valuesArray[section];
    return arra.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.addOrderModel.IdentitfyCategory isEqual:@"1"]) {
        LKWorkOrderListDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKWorkOrderListDetailCell"];
        cell.titileLabel.text = self.valuesArray[indexPath.section][indexPath.row];
        cell.confirmImageView.hidden = YES;
        cell.arrowBtn.hidden = YES;
        if (indexPath.row == 0) {
            cell.confirmImageView.hidden = NO;
            if ([self.addOrderModel.CusIsAuthenticated boolValue]) {
                cell.confirmImageView.image = [UIImage imageNamed:@"confirmBankCard"];
                return cell;
            }else{
                cell.confirmImageView.image = [UIImage imageNamed:@"unconfirmBankCard"];
                return cell;
            }
        }
        if (indexPath.row == 1) {
            
            cell.lineView.hidden = NO;

        }else{
            cell.lineView.hidden = YES;
        }
        NSArray *arra = self.valuesArray[indexPath.section];
        
        if (indexPath.row == arra.count - 1) {
//            LKWorkOrderListDetailPictureCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKWorkOrderListDetailPictureCell"];
            cell.titileLabel.text = self.valuesArray[indexPath.section][indexPath.row];
            if (indexPath.section == 0) {
//                [cell.pictureImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",ServerlUrl,self.addOrderModel.PayAccountPhotoUrl]] placeholderImage:[UIImage imageNamed:@"pic"]];
            }else{
//                [cell.pictureImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",ServerlUrl,self.addOrderModel.RepayAccountPhotoUrl]] placeholderImage:[UIImage imageNamed:@"pic"]];
            }
            
            return cell;
        }
        return cell;
    }else{
        LKWorkOrderListDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKWorkOrderListDetailCell"];
        cell.titileLabel.text = self.valuesArray[indexPath.section][indexPath.row];
        cell.arrowBtn.hidden = YES;
        cell.confirmImageView.hidden = YES;
        if (indexPath.row == 0) {
            cell.confirmImageView.hidden = NO;
            if ([self.addOrderModel.CusIsAuthenticated boolValue]) {
                cell.confirmImageView.image = [UIImage imageNamed:@"confirmBankCard"];
            }else{
                cell.confirmImageView.image = [UIImage imageNamed:@"unconfirmBankCard"];
            }
        }

        if (indexPath.row == 1) {
            cell.lineView.hidden = NO;
        }else{
            cell.lineView.hidden = YES;
        }
        NSArray *arra = self.valuesArray[indexPath.section];
        
        if (indexPath.row == arra.count - 1) {
            LKWorkOrderListDetailPictureCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKWorkOrderListDetailPictureCell"];
            cell.titleLabel.text = self.valuesArray[indexPath.section][indexPath.row];
            if (indexPath.section == 0) {
                [cell.pictureImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",ServerlUrl,self.addOrderModel.PayAccountPhotoUrl]] placeholderImage:[UIImage imageNamed:@"pic"]];
            }else{
                [cell.pictureImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",ServerlUrl,self.addOrderModel.RepayAccountPhotoUrl]] placeholderImage:[UIImage imageNamed:@"pic"]];
            }
            
            return cell;
        }
        return cell;
    }
 
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *arra = self.valuesArray[indexPath.section];
    if ([self.addOrderModel.IdentitfyCategory isEqual:@"1"]) {
        return 40;
    }else{
        if (indexPath.row == arra.count - 1) {
            return 90;
        }
        return 43;
    }

}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    LKOrderEditTopView *topView = [[[NSBundle mainBundle] loadNibNamed:@"LKOrderEditTopView" owner:self options:nil] lastObject];
    [topView.leftBtn setTitle:self.titlesArray[section] forState:UIControlStateNormal];
    [topView.leftBtn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [topView.rightBtn addTarget:self action:@selector(chooseBankCard:) forControlEvents:UIControlEventTouchUpInside];
    [topView.rightBtn setImage:[UIImage imageNamed:@"productChoose"] forState:UIControlStateNormal];
    [topView.rightBtn setTitle:@" 选择" forState:UIControlStateNormal];
    topView.rightBtn.tag = 10000 + section;
    if (self.isOrderUpdate) {
        if (self.isOrderBianGeng) {
            topView.rightBtn.hidden = YES;
            if (section == 1) {
              topView.rightBtn.hidden = NO;
            }else{
              topView.rightBtn.hidden = YES;
            }
        }else{
            topView.rightBtn.hidden = YES;
            if (section == 0) {
               topView.rightBtn.hidden = NO;
            }else{
                topView.rightBtn.hidden = YES;
            }
            
        }
    }else{
        if (section == 1) {
            topView.rightBtn.hidden = YES;
        }else{
            topView.rightBtn.hidden = NO;
        }
    }
    return topView;
}
-(void)chooseBankCard:(UIButton *)btn{
    WS(weakSelf)
    LKChooseBankCardListViewController *vc = [[LKChooseBankCardListViewController alloc] init];
    vc.customermodel = self.customermodel;
    
    vc.addOrderModel = self.addOrderModel;
    vc.isOrderUpdate = self.isOrderUpdate;
    vc.selectSection = btn.tag - 10000;
    vc.sendArraBlock = ^(NSArray *valuesArra, LKAddOrderModel *model) {
        weakSelf.addOrderModel = model;
        [weakSelf.valuesArray replaceObjectAtIndex:btn.tag - 10000 withObject:valuesArra];
//        NSMutableArray *selectArra = [[NSMutableArray alloc] init];
//        for (NSInteger i = valuesArra.count / 2; i < valuesArra.count; i++) {
//            [selectArra addObject:valuesArra[i]];
//        }
        [weakSelf.valuesArray replaceObjectAtIndex:1 withObject:valuesArra];
//        [weakSelf.tableview reloadSection:btn.tag -10000 withRowAnimation:UITableViewRowAnimationNone];
        [weakSelf.tableview reloadData];
    };
    [self.navigationController pushViewController:vc animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 43;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

}
-(NSMutableArray *)valuesArray{
    if (!_valuesArray) {
        _valuesArray = [[NSMutableArray alloc] init];
        [_valuesArray addObjectsFromArray:@[@[],@[]]];
    }
    return _valuesArray;
}
-(NSArray *)titlesArray{
    if (!_titlesArray) {
        _titlesArray = @[@"划扣账户",@"回款账户"];
    }
    return _titlesArray;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
